﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:04
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  SettingRepository : Repository<Setting, Setting, TbSetting>, ISettingRepository<Setting>
    {
        public override Setting CreateEntity()
        {
            return new Setting { };
        }
        public override IQueryable<Setting> Get()
        {
            return MapQueryable(Entities.TbSetting);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbSetting).Count();
        }
        public override IQueryable<Setting> Get(Expression<Func<Setting, bool>> expression)
        {
            return MapQueryable(Entities.TbSetting).Where(expression);
        }
        public override IQueryable<Setting> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbSetting), sort, order);
        }
        public override IQueryable<Setting> GetSort(Expression<Func<Setting, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbSetting).Where(expression), sort, order);
        }
        public override void Add(Setting t)
        {
            Entities.AddToTbSetting(ReverseMapModel(t));
        }
        public override void Update(Setting t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Setting t)
        {
            if (Entities.TbSetting.Any(o => o.CoSettingId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbSetting(ReverseMapModel(t));
            }
        }
        public override void Delete(Setting t)
        {
            Entities.TbSetting.Where(o => o.CoSettingId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Setting, bool>> expression)
        {
            MapQueryable(Entities.TbSetting).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Setting> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Setting>();
        }
      
        public override sealed IQueryable<Setting> MapQueryable(IQueryable<TbSetting> queryable)
        {
            return queryable.Select(o => new Setting 
            {
         
	           Id=o.CoSettingId,
	           Code = o.CoCode 
,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           ContactUs = o.CoContactUs 
,
	           Copyright = o.CoCopyright 
,
	           CompanyAdress = o.CoCompanyAdress 
,
	           Fax = o.CoFax 
,
	           Email = o.CoEmail 
,
	           Telnet = o.CoTelnet 
,
	           Mobile = o.CoMobile 
,
	           Qq = o.CoQq 
,
	           AccessCountHtml = o.CoAccessCountHtml 
,
	           Recruitment = o.CoRecruitment 

,
	           CompanyName = o.CoCompanyName 
,
	           SalesNetwork = o.CoSalesNetwork 
,
	           CustomerServiceTelnet = o.CoCustomerServiceTelnet 
,
	           HotLineTelnet = o.CoHotLineTelnet 
,
	           RootUrl = o.CoRootUrl 
,
	           Title = o.CoTitle 
,
	           CssUrl = o.CoCssUrl 
,
	           MetaKeywords = o.CoMetaKeywords 
,
	           MetaDescription = o.CoMetaDescription 
,
	           OrderAction = o.CoOrderAction 
,
	           OrderEmail = o.CoOrderEmail 
,
	           OrderCompanyName = o.CoOrderCompanyName 
,
	           CompanyProfile = o.CoCompanyProfile 
,
	           Video = o.CoVideo 
,
	           Contacter = o.CoContacter 
,
	           MessageEmail = o.CoMessageEmail 
,
	           MessageName = o.CoMessageName 
,
	           RegistrationAgreement = o.CoRegistrationAgreement 
,
	           Help = o.CoHelp 
,
	           PrivacyPolicy = o.CoPrivacyPolicy 
,
	           TermsOfUse = o.CoTermsOfUse 
,
	           SiteMap = o.CoSiteMap 
,
	           ZipCode = o.CoZipCode 
,
	           ImageUrl = o.CoImageUrl 
,
	           JsUrl = o.CoJsUrl 
,
	           Contact = o.CoContact 
,
	           LegalRepresentative = o.CoLegalRepresentative 
,
	           ShareHtml = o.CoShareHtml 
            
            });
        }
      
        public override sealed IQueryable<TbSetting> ReverseMapQueryable(IQueryable<Setting> queryable)
        {
            return queryable.Select(o => new TbSetting 
            { 
        
	           CoSettingId=o.Id,
	           CoCode = o.Code
,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoContactUs = o.ContactUs
,
	           CoCopyright = o.Copyright
,
	           CoCompanyAdress = o.CompanyAdress
,
	           CoFax = o.Fax
,
	           CoEmail = o.Email
,
	           CoTelnet = o.Telnet
,
	           CoMobile = o.Mobile
,
	           CoQq = o.Qq
,
	           CoAccessCountHtml = o.AccessCountHtml
,
	           CoRecruitment = o.Recruitment


,
	           CoCompanyName = o.CompanyName
,
	           CoSalesNetwork = o.SalesNetwork
,
	           CoCustomerServiceTelnet = o.CustomerServiceTelnet
,
	           CoHotLineTelnet = o.HotLineTelnet
,
	           CoRootUrl = o.RootUrl
,
	           CoTitle = o.Title
,
	           CoCssUrl = o.CssUrl
,
	           CoMetaKeywords = o.MetaKeywords
,
	           CoMetaDescription = o.MetaDescription
,
	           CoOrderAction = o.OrderAction
,
	           CoOrderEmail = o.OrderEmail
,
	           CoOrderCompanyName = o.OrderCompanyName
,
	           CoCompanyProfile = o.CompanyProfile
,
	           CoVideo = o.Video
,
	           CoContacter = o.Contacter
,
	           CoMessageEmail = o.MessageEmail
,
	           CoMessageName = o.MessageName
,
	           CoRegistrationAgreement = o.RegistrationAgreement
,
	           CoHelp = o.Help
,
	           CoPrivacyPolicy = o.PrivacyPolicy
,
	           CoTermsOfUse = o.TermsOfUse
,
	           CoSiteMap = o.SiteMap
,
	           CoZipCode = o.ZipCode
,
	           CoImageUrl = o.ImageUrl
,
	           CoJsUrl = o.JsUrl
,
	           CoContact = o.Contact
,
	           CoLegalRepresentative = o.LegalRepresentative
,
	           CoShareHtml = o.ShareHtml
  
            
            });
        }
      
        public override sealed Setting MapModel(TbSetting model)
        {
            return new Setting {
	           Id=model.CoSettingId,
	           Code = model.CoCode
,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           ContactUs = model.CoContactUs
,
	           Copyright = model.CoCopyright
,
	           CompanyAdress = model.CoCompanyAdress
,
	           Fax = model.CoFax
,
	           Email = model.CoEmail
,
	           Telnet = model.CoTelnet
,
	           Mobile = model.CoMobile
,
	           Qq = model.CoQq
,
	           AccessCountHtml = model.CoAccessCountHtml
,
	           Recruitment = model.CoRecruitment

	       
,
	           CompanyName = model.CoCompanyName
,
	           SalesNetwork = model.CoSalesNetwork
,
	           CustomerServiceTelnet = model.CoCustomerServiceTelnet
,
	           HotLineTelnet = model.CoHotLineTelnet
,
	           RootUrl = model.CoRootUrl
,
	           Title = model.CoTitle
,
	           CssUrl = model.CoCssUrl
,
	           MetaKeywords = model.CoMetaKeywords
,
	           MetaDescription = model.CoMetaDescription
,
	           OrderAction = model.CoOrderAction
,
	           OrderEmail = model.CoOrderEmail
,
	           OrderCompanyName = model.CoOrderCompanyName
,
	           CompanyProfile = model.CoCompanyProfile
,
	           Video = model.CoVideo
,
	           Contacter = model.CoContacter
,
	           MessageEmail = model.CoMessageEmail
,
	           MessageName = model.CoMessageName
,
	           RegistrationAgreement = model.CoRegistrationAgreement
,
	           Help = model.CoHelp
,
	           PrivacyPolicy = model.CoPrivacyPolicy
,
	           TermsOfUse = model.CoTermsOfUse
,
	           SiteMap = model.CoSiteMap
,
	           ZipCode = model.CoZipCode
,
	           ImageUrl = model.CoImageUrl
,
	           JsUrl = model.CoJsUrl
,
	           Contact = model.CoContact
,
	           LegalRepresentative = model.CoLegalRepresentative
,
	           ShareHtml = model.CoShareHtml
            };
        }
        
        public override sealed TbSetting ReverseMapModel(Setting model)
        {
            return new TbSetting { 
	           CoSettingId=model.Id,
	           CoCode = model.Code
	        
,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoContactUs = model.ContactUs
	        
,
	           CoCopyright = model.Copyright
	        
,
	           CoCompanyAdress = model.CompanyAdress
	        
,
	           CoFax = model.Fax
	        
,
	           CoEmail = model.Email
	        
,
	           CoTelnet = model.Telnet
	        
,
	           CoMobile = model.Mobile
	        
,
	           CoQq = model.Qq
	        
,
	           CoAccessCountHtml = model.AccessCountHtml
	        
,
	           CoRecruitment = model.Recruitment
	        

	          
	        
,
	           CoCompanyName = model.CompanyName
	        
,
	           CoSalesNetwork = model.SalesNetwork
	        
,
	           CoCustomerServiceTelnet = model.CustomerServiceTelnet
	        
,
	           CoHotLineTelnet = model.HotLineTelnet
	        
,
	           CoRootUrl = model.RootUrl
	        
,
	           CoTitle = model.Title
	        
,
	           CoCssUrl = model.CssUrl
	        
,
	           CoMetaKeywords = model.MetaKeywords
	        
,
	           CoMetaDescription = model.MetaDescription
	        
,
	           CoOrderAction = model.OrderAction
	        
,
	           CoOrderEmail = model.OrderEmail
	        
,
	           CoOrderCompanyName = model.OrderCompanyName
	        
,
	           CoCompanyProfile = model.CompanyProfile
	        
,
	           CoVideo = model.Video
	        
,
	           CoContacter = model.Contacter
	        
,
	           CoMessageEmail = model.MessageEmail
	        
,
	           CoMessageName = model.MessageName
	        
,
	           CoRegistrationAgreement = model.RegistrationAgreement
	        
,
	           CoHelp = model.Help
	        
,
	           CoPrivacyPolicy = model.PrivacyPolicy
	        
,
	           CoTermsOfUse = model.TermsOfUse
	        
,
	           CoSiteMap = model.SiteMap
	        
,
	           CoZipCode = model.ZipCode
	        
,
	           CoImageUrl = model.ImageUrl
	        
,
	           CoJsUrl = model.JsUrl
	        
,
	           CoContact = model.Contact
	        
,
	           CoLegalRepresentative = model.LegalRepresentative
	        
,
	           CoShareHtml = model.ShareHtml
	        
            
            
            };
        }


    }
}



